//
//  HMWelcomeViewController.swift
//  大深圳微博01
//
//  Created by shenzhenIOS on 16/4/11.
//  Copyright © 2016年 shenzhenIOS. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

// Swift定义枚举, 使用原始值 0
enum Season: Int {
    case Spring = 0
    case Summer
    case Autumn
    case Winter
}


//        let season = Season.Spring
//        let season2 = Season(rawValue: 250)
//        HMPrint("season2: \(season2)")

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        // 之前有没有用户信息
        setIcon()
        
        // 发送网络请求,获取用户信息
        HMUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                HMPrint("获取用户信息出错了: \(error)")
                return
            }
            
            // 获取用户信息成功了
            self.setIcon()
        }
    }
    
    private func setIcon() {
        if let avatar_large = HMUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            // 网络请求完了在调用这段代码
            // 使用SD来下载图片
            let url = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    private func prepareUI() {
        // 1.添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景 Masonry SnapKit
        bkgImageView.snp_makeConstraints { (make) -> Void in
            // make.的leading参照view的.leading: 注意参照的view的属性需要添加snp_
//            make.leading.equalTo(view.snp_leading)
//            make.top.equalTo(view.snp_top)
//            make.trailing.equalTo(view.snp_trailing)
//            make.bottom.equalTo(view.snp_bottom)
            
            // 缩写1,当约束的属性和参照的属性一样的时,参照的属性可以省略
//            make.leading.equalTo(view)
//            make.top.equalTo(view)
//            make.trailing.equalTo(view)
//            make.bottom.equalTo(view)
            
            // 缩写2:当参照都一样的时候可以连在一起写
//            make.leading.top.trailing.bottom.equalTo(view)
            
            // 填充父控件简单方式
//            make.edges.equalTo(UIEdgeInsets(top: 10, left: 10, bottom: -10, right: -10))
//            make.edges.equalTo(view)
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(view.snp_centerX)
            // 位置约束
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            
            // 尺寸约束
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        // label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    // 在view看的见的时候在做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewAnimation()
    }
    
    /// 头像动画
    private func iconViewAnimation() {
        // 修改头像底部约束,动画
        // snp_updateConstraints: 修改约束,不会立马根据约束去修改frame的值
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
        
        // usingSpringWithDamping: 弹簧明显程度: 0 - 1, 数值越小越明显
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(1, delay:0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 通知系统来重新设置布局，layoutIfNeeded立马根据约束去修改frame的值
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 让label显示出来
                UIView.animateWithDuration(HMDefautlAnimDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                        HMPrint("动画完成了,要切换界面")
                        // 切换到mainViewController
                        let controller = HMMainViewController()
//                        UIApplication.sharedApplication().keyWindow?.rootViewController = controller
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
                })
        }
    }

    // MARK: - 懒加载
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 圆角 170(2x) 85
        imageView.layer.cornerRadius = 42.5
        // 裁剪
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 欢迎label
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(HMWelcomeLabelFontSize)
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()
}
